//
//  QCFireController.swift
//  爱汽车
//
//  Created by liujianlong on 2023/8/24.
//

import UIKit

class QCFireController: QCViewController {
    var array = [[PrePos]]()
    var headers = ["点火后设置联动预置位:","熄火后设置联动预置位:"]
    var select_dw = [0,0]
    override func viewDidLoad() {
        super.viewDidLoad()
        initUI()
    }
    
    override func initUI() {
        title = LS("点火熄火设置")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(QCSettingCell.nib(), forCellReuseIdentifier: QCSettingCell.ID())
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    override func initData() {
        super.initData()
        let array_on = [PrePos(name: "点火设置", num: 0)] + PrePosArray_4
        let array_off = [PrePos(name: "熄火设置", num: 0)] + PrePosArray_5
        array = [array_on,array_off]
    }
    
    override var bytes: [UInt8]{
        didSet{
            let ack = bytes[7]
            if ack != UInt8(AIQC.userInfo.ack){return}
            let data = [UInt8](bytes[AI_index..<bytes.count])
            if recived == false {
                recived = true
                return}
            select_dw = data[4...5].map{Int($0)}
            tableView.reloadData()
        }
    }
    
    func action(_ index:IndexPath){
        recived = false
        let prepos = array[index.section][index.row]
        select_dw[index.section] = prepos.num > 0 ? prepos.num : select_dw[index.section] == 0 ? array[index.section].count : 0
        tableView.reloadData()
        
        if select_dw[index.section] >= array[index.section].count{ return}
        let hex = select_dw[index.section] > 0 ? HEX(UInt8(prepos.num)) : "00"
        let cmd = index.section == 0 ? AIQC.setTurnOn.cmd : AIQC.setTrunOff.cmd
        BLEData.appSetDataToPer(cmd, hex).write
    }
}
//MARK: - UITableViewDataSource
extension QCFireController:UITableViewDataSource
{
    func numberOfSections(in tableView: UITableView) -> Int {
        return array.count
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return select_dw[section] > 0 ? array[section].count : 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: QCSettingCell.ID()) as! QCSettingCell
        let prepos = array[indexPath.section][indexPath.row]
        let select_i = select_dw[indexPath.section]
        cell.title.text = LS(prepos.name)
        cell.type = indexPath.row == 0 ? .switchBtn(select_i > 0) : .subImg(select_i == prepos.num ? "选择" : "未选择")
        cell.block = {[weak self] ison in
            self?.action(indexPath)
        }
        return cell
    }
}
//MARK: - UITableViewDelegate
extension QCFireController:UITableViewDelegate{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0{return}
        action(indexPath)
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = UIView()
        view.backgroundColor = .clear
        let lbl = UILabel()
        lbl.text = LS(headers[section])
        lbl.font = UIFont.systemFont(ofSize: 14)
        lbl.textHexColor = "#AEAEAE"
        view.addSubview(lbl)
        lbl.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(20)
        }
        return view
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
